<?php
declare(strict_types=1);

namespace plugin\ky_black\app\admin\controller;

use support\Request;
use support\Response;
use plugin\ky_black\app\admin\model\Partner;

class PartnerController extends BaseController
{
    /**
     * 获取合作商家列表
     * @param Request $request
     * @return Response
     */
    public function list(Request $request): Response
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $keyword = $request->input('keyword', '');
        
        $query = Partner::query();
        
        if ($keyword) {
            $query->where('name', 'like', "%{$keyword}%")
                  ->orWhere('contact', 'like', "%{$keyword}%");
        }
        
        $total = $query->count();
        $list = $query->offset(($page - 1) * $limit)
                      ->limit($limit)
                      ->orderBy('id', 'desc')
                      ->get();
        
        return $this->success([
            'list' => $list,
            'total' => $total
        ]);
    }

    /**
     * 添加合作商家
     * @param Request $request
     * @return Response
     */
    public function add(Request $request): Response
    {
        $data = $request->post();
        $rules = [
            'name' => 'required|string|max:50',
            'contact' => 'required|string|max:20',
            'phone' => 'required|string|max:20',
            'address' => 'required|string|max:255',
            'business_license' => 'required|string',
            'status' => 'required|integer|in:0,1'
        ];
        
        $this->validate($data, $rules);
        
        $partner = new Partner();
        $partner->fill($data);
        $partner->save();
        
        return $this->success();
    }

    /**
     * 更新合作商家信息
     * @param Request $request
     * @return Response
     */
    public function edit(Request $request): Response
    {
        $id = $request->input('id');
        $data = $request->post();
        
        $rules = [
            'name' => 'required|string|max:50',
            'contact' => 'required|string|max:20',
            'phone' => 'required|string|max:20',
            'address' => 'required|string|max:255',
            'business_license' => 'required|string',
            'status' => 'required|integer|in:0,1'
        ];
        
        $this->validate($data, $rules);
        
        $partner = Partner::find($id);
        if (!$partner) {
            return $this->error('合作商家不存在');
        }
        
        $partner->fill($data);
        $partner->save();
        
        return $this->success();
    }

    /**
     * 删除合作商家
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        $id = $request->input('id');
        
        $partner = Partner::find($id);
        if (!$partner) {
            return $this->error('合作商家不存在');
        }
        
        $partner->delete();
        
        return $this->success();
    }

    /**
     * 更新合作商家状态
     * @param Request $request
     * @return Response
     */
    public function status(Request $request): Response
    {
        $id = $request->input('id');
        $status = $request->input('status');
        
        $partner = Partner::find($id);
        if (!$partner) {
            return $this->error('合作商家不存在');
        }
        
        $partner->status = $status;
        $partner->save();
        
        return $this->success();
    }
} 